package com.leilei.entity;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: lei
 * @date: 2025-05-21 11:28
 * @desc: Starter 自动配置类，配置加解密拦截器，默认生效 以及在 encrypt.enabled = true 时生效
 */
@Configuration
@ConditionalOnProperty(prefix = "field-encrypt", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(value = {EncryptProperties.class})
@Slf4j
public class FieldEncryptAutoConfiguration {

    @Bean
    public Sm4Encryptor sm4Encryptor(EncryptProperties properties) {
        return new Sm4Encryptor(properties);
    }

    @Bean
    public EncryptProcessor encryptFieldProcessor(Sm4Encryptor encryptor) {
        log.info("加载字段加解密处理器");
        return new EncryptProcessor(encryptor);
    }

    @Bean
    public Interceptor sm4EncryptInterceptor(EncryptProcessor processor) {
        log.info("加载字段加密拦截器");
        return new Sm4EncryptInterceptor(processor);
    }

    @Bean
    public Interceptor sm4DecryptInterceptor(EncryptProcessor processor) {
        log.info("加载字段解密拦截器");
        return new Sm4DecryptInterceptor(processor);
    }
}
